package xin.yangshuai.springsecurity01.config;

import com.alibaba.fastjson2.JSON;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import xin.yangshuai.common01.entity.BaseResult;

import java.io.IOException;

public class MyAuthenticationSuccessHandler implements AuthenticationSuccessHandler {

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {

        // 获取用户身份信息
        Object principal = authentication.getPrincipal();

        // 获取用户凭证信息，通过密码形式登录，这个凭证就是密码
        // Object credentials = authentication.getCredentials();

        // 获取用户权限信息
        // Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();

        BaseResult<Object> result = new BaseResult<>();
        result.setCode("0");
        result.setMessage("登录成功");
        result.setData(principal);

        String json = JSON.toJSONString(result);

        response.setContentType("application/json;charset=UTF-8");
        response.getWriter().println(json);
    }
}
